/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 * Document   : ajax.jsp
 * Created on : 18/06/2013, 04:04:13 PM
 * Author     : Javier Aviles Cruz
 *
*/
var that = this;
that._globalResponse = null;

win = null;

/*
* function GetXmlHttpObject
*	Devuelve el objeto Ajax, con el haremos peticiones asincronas  
*
*/

function GetXmlHttpObject(){
	
	var xmlHttp = null;
	
	try	{    // Firefox, Opera 8.0+, Safari    
	  	xmlHttp = new XMLHttpRequest();
	} catch (e)  {    // Internet Explorer    
		try {      
			xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" ); 
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch (e) {
					alert( "Your browser does not support AJAX!" );
					return false;
			}      
		}    
	}	
    return xmlHttp; 
}


/*
*	Function sendRequest
*	Para administrar cualquier peticion asincrona
*	url = dispatch
*	func = function que maneja la respuesta
*	args = arreglo de parametros
*	resp = tipo de respuesta text o xml.
*/

function sendRequest( url , func , args, resp , ext){
	updateTime(); 
	var action = url;
	var xmlHttp = new GetXmlHttpObject();
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4){
 		  if (xmlHttp.status == 200) {	
				var response = ( resp.toLowerCase() == 'text' )? xmlHttp.responseText : xmlHttp.responseXML;				
				if (response != null){	
					that._globalResponse = null;
					that._globalResponse = response	;
					if( func != '') window[ func ](ext);
				}				
			}else {
				alert('There was a problem with the request. Func: ' + func + ' action: ' + action);
				return 0;
			}
 		}
  	  }
  	 if( args.length > 0 ){
	  	for( i=0; i < args.length; i++ ){
			action += "&" + args[i][0] + "=" + args[i][1] 
		}
	}
	//alert('desde ajax '+action);
	xmlHttp.open( "POST" , action + "&random=" + Math.random() , true );
	xmlHttp.send(null);
}

